package com.hxci.zc.controller;

import com.hxci.zc.domain.Student;
import com.hxci.zc.service.StudentService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("stu")
public class StudentController {
    @Autowired
    StudentService service;


    //@ResponseBody //处理ajax 请求的
    @RequestMapping("query")
    public String query(Model model){
        List<Student> list = service.list();
        System.out.println(list);
        model.addAttribute("list",list);
        return "student";//请求转发（传值）
    }


    @RequestMapping("preAdd")
    public String preAdd(){

        return "add";//请求转发（传值）
    }
    @RequestMapping("add")
    public String add(Student student){
        service.saveOrUpdate(student);
        return "redirect:/stu/query";//请求转发（传值）
    }
    @RequestMapping("del")
    public String del(String id ){
        service.removeById(id);
        return "redirect:/stu/query";//请求转发（传值）
    }

    @RequestMapping("edit")
    public String edit(String id,Model model){
        Student stu = service.getById(id);
        model.addAttribute("stu",stu);
        return "add";//请求转发（传值）
    }
}
